<?php
namespace app\api\controller;

use app\api\controller\Base;
use app\api\model\UserPhotoWall;

class User extends Base
{
    // 修改昵称
    public function changeNickname()
    {
        if (request()->isPost()) {
            $data = Request()->param();
            if (!$this->con_validate->scene('checkNickname')->check($data)) {
                return json_return(301, $this->con_validate->getError());
            }
            $data['remote_device'] = Request()->header('remote-device');
            if (!$data['remote_device']) {return json_return(301, '没有获取终端设备号');}
            // 判断该用户是否修改过昵称
            $is_change = model('User')->get($data['user_id']);
            if (!$is_change) {return json_return(302, '用户信息不存在');}
            if ($is_change->is_change_name == 1) {
                return json_return(303, '用户已经修改过昵称了');
            }
            $res = $is_change->save(['name' => $data['user_nickname'], 'is_change_name' => 1]);
            $this->common->writeLogRecord(['type' => 2, 'user_id' => $data['user_id'], 'info' => " 修改昵称 ： " . $data['user_nickname'], 'login_ip' => Request()->ip()]); // 记录修改日志
            return $res ? json_return(200, '修改昵称成功') : json_return(304, '修改昵称失败，请联系管理员');
        } else {
            return json_return(399, '非法请求');
        }
    }

    // 修改年龄或者个性签名
    public function changeAgeOrSignature()
    {
        if (Request()->isPost()) {
            $data = Request()->param();
            if (!$this->con_validate->scene('checkAgeOrSignature')->check($data)) {
                return json_return(301, $this->con_validate->getError());
            }
            $data['remote_device'] = Request()->header('remote-device');
            if (!$data['remote_device']) {return json_return(301, '没有获取终端设备号');}
            if (!model('User')->get($data['user_id'])) {return json_return(303, '用户信息不存在');}
            if (!isset($data['user_age']) && !isset($data['user_signature'])) {return json_return(302, '年龄和个性签名必须存在一个。');}
            $save_data = [];
            if (isset($data['user_age'])) {
                $save_data['age'] = $data['user_age'];
            }
            if (isset($data['user_signature'])) {
                $save_data['signature'] = $data['user_signature'];
            }
            $res = model('User')->get($data['user_id'])->allowField(true)->save($save_data);
            $this->common->writeLogRecord(['type' => 2, 'user_id' => $data['user_id'], 'login_ip' => Request()->ip(), 'info' => " 修改个人信息 ： " . json_encode($save_data)]); // 记录修改日志
            return $res ? json_return(200, '修改信息成功') : json_return(304, '修改信息失败，请联系管理员');
        } else {
            return json_return(399, '非法请求');
        }

    }

    // 修改用户手机号
    public function changePhone()
    {
        if (Request()->isPost()) {
            $data = Request()->param();
            if (!$this->con_validate->scene('checkChangePhone')->check($data)) {
                return json_return(301, $this->con_validate->getError());
            }
            $data['remote_device'] = Request()->header('remote-device');
            if (!$data['remote_device']) {return json_return(301, '没有获取终端设备号');}
            $user_info = model('User')->where(['id' => $data['user_id'], 'is_del' => 0])->find();
            if (!$user_info) {return json_return(302, '没有该用户');}
            // 验证验证码
            // $res_code = $this->zero_touch->checkVerify($data['phone'], $data['code']); // 验证验证码
            $res_code = true;
            if ($res_code == true) {
                $save_data['phone'] = $data['phone'];
                $res                = $user_info->save($save_data);
                $this->common->writeLogRecord(['type' => 2, 'user_id' => $data['user_id'], 'login_ip' => Request()->ip(), 'info' => " 修改个人信息 ： " . json_encode($save_data)]); // 记录修改日志
                return $res ? json_return(200, '修改手机成功') : json_return(304, '修改信息失败，请联系管理员');
            } else {
                return json_return(303, '验证码错误');
            }

        } else {
            return json_return(399, '非法请求');
        }

    }

    // 上传照片墙
    public function uploadPhotoWall()
    {
        if (Request()->isPost()) {
            $data = Request()->param();
            if (!$this->con_validate->scene('checkUploadPhotoWall')->check($data)) {
                return json_return(301, $this->con_validate->getError());
            }
            $data['remote_device'] = Request()->header('remote-device');
            if (!$data['remote_device']) {return json_return(301, '没有获取终端设备号');}
            $files    = request()->file('images');
            $up_count = count($files);
            $al_count = model('UserPhotoWall')->where(['user_id' => $data['user_id'], 'is_del' => 0])->count();
            if ($up_count + $al_count > 9) {
                return json_return(303, '用户最多还能上传' . (9 - $al_count) . "张图片", ['number' => 9 - $al_count]);
            }
            if (!$files) {return json_return(302, '未获取上传的照片墙图片');}
            $res        = $this->uploadsFiles($files, 600, 400, '/uploads/user/photo_wall/');
            $photo_wall = new UserPhotoWall();
            $lists      = [];
            foreach ($res as $key => $value) {
                $lists[] = ['user_id' => $data['user_id'], 'photo' => $value];
            }
            $photo_wall->saveAll($lists);
            return json_return(200, "保存照片墙成功", model('UserPhotoWall')->where(['user_id' => $data['user_id'], 'is_del' => 0])->order('id desc')->limit(9)->select()->toArray());
        } else {
            return json_return(399, '非法请求');
        }
    }

    // 获取用户照片墙
    public function getPhotoWall()
    {
        if (request()->isGet()) {
            $data = Request()->param();
            if (!$this->con_validate->scene('getPhotoWall')->check($data)) {
                return json_return(301, $this->con_validate->getError());
            }
            $data['remote_device'] = Request()->header('remote-device');
            if (!$data['remote_device']) {return json_return(301, '没有获取终端设备号');}
            $photos = model('UserPhotoWall')->where(['user_id' => $data['user_id'], 'is_del' => 0])->order('id desc')->limit(9)->select()->toArray();
            return $photos ? json_return(200, '获取用户照片墙', ['count' => count($photos), 'photos' => $photos]) : json_return(302, '获取用户照片墙失败');
        } else {
            return json_return(399, '非法请求');
        }

    }

    // 删除照片墙
    public function delPhotoWall()
    {
        if (request()->isPost()) {
            $data = Request()->param();
            if (!$this->con_validate->scene('delPhotoWall')->check($data)) {
                return json_return(301, $this->con_validate->getError());
            }
            $data['remote_device'] = Request()->header('remote-device');
            if (!$data['remote_device']) {return json_return(301, '没有获取终端设备号');}
            $photo_info = model('UserPhotoWall')->get($data['photo_id']);
            if ($photo_info->user_id !== $data['user_id']) {
                return json_return(302, '不能删除非自己的照片');
            }
            return $photo_info->save(['is_del' => 1]) ? json_return(200, '删除照片成功') : json_return(303, "删除照片失败，请联系管理员");
        } else {
            return json_return(399, '非法请求');
        }
        
    }

}
